package com.projectfile.controller;


import com.projectfile.entity.LoginUser;
import com.projectfile.model.JsonResult;

import com.projectfile.service.CustomUserService;
import com.projectfile.service.LoginServiceImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;

import javax.servlet.http.HttpServletResponse;


@RestController
@CrossOrigin
public class LoginController {

    @Resource
    private LoginServiceImpl loginService;
    @Resource
    private CustomUserService customUserService;
    @ResponseBody
    @GetMapping("/login/{username}")
    public JsonResult login(@PathVariable("username") String username){
        JsonResult res;
        LoginUser user=loginService.findByUserName(username);
        System.out.println(user.getUser_id()+"   saaaaa  "+user.getUsername()+"    "+user.getPassword());

        System.out.println("notice到controller");
        if(user==null){
            System.out.println("登录没成功");
            res=new JsonResult(500,"没有登录成功");
        }else{
            System.out.println("登陆成功");
            res=new JsonResult(200,"登录成功");
            res.setData(user);

        }

        return res;
    }
    /*
       在Cookies中存储数据
     */
    @ResponseBody
    @RequestMapping(value = "/setCookies",method = RequestMethod.POST)
    public  String setCookies(HttpServletResponse response){
        Cookie cookie=new Cookie("cookie","微信公众号：骄傲的程序员");
//        cookie.setMaxAge(60);             //存活一分钟
//        cookie.setMaxAge(60*60);          //存活一小时
//        cookie.setMaxAge(24*60*60);       //存活一天
        cookie.setMaxAge(365*24*60*60);     //存活一年
        response.addCookie(cookie);
        return "添加成功";
    }
//    /*
//    调用Cookies
//     */
//    @ResponseBody
//    @RequestMapping(value = "/getCookies",method = RequestMethod.GET)
//    public String getCookies(HttpServletRequest request){
//        Cookie[] cookies =  request.getCookies();//获取保存在request的所有cookie
//        if(cookies != null){//判断cookies数组是否为空
//            for(Cookie cookie : cookies){
//                if(cookie.getName().equals("cookie")){//通过for循环找到想要获取的值
//                    System.out.println(cookie.getValue());//控制台输出
//                    return cookie.getValue();//返回cookie
//                }
//            }
//        }
//        return  null;
//    }


}
